import mysql.connector


class DBUtil:

    def __init__(self, cursor):
        self.cursor = cursor
        cursor.execute('create table person (name varchar(20), sex number)')

    def insert(self, person):
        self.cursor.execute('insert into person (name, sex) values(\'{0}\', \'{1}\')'.format(person.name, person.sex))

    def delete(self, person):
        self.cursor.execute('delete from person where name=\'{0}\''.format(person.name))
        print('rowcount={0}'.format(self.cursor.rowcount))

    def update(self, person):
        self.cursor.execute('update person set sex=\'{0}\' where name=\'{1}\''.format(person.sex, person.name))
        print('rowcount={0}'.format(self.cursor.rowcount))

    def query(self):
        self.cursor.execute('select * from person')
        ps = self.cursor.fetchall()
        print(ps)


class Person:
    def __init__(self, name, sex):
        self.name = name
        self.sex = sex

    def __str__(self):
        return 'name=%s, sex=%s'.format(self.name, self.sex)


if __name__ == '__main__':
    connection = mysql.connector.connect(user='root', password='', database='test')
    c = connection.cursor()

    c.execute('create table user (id varchar(20) primary key, name varchar(20))')
    c.execute('insert into user (id, name) values (%s, %s)', ['13', 'Michael3'])
    print('count={0}'.format(c.rowcount))
    connection.commit()

    c.execute('select * from user')
    ps = c.fetchall()
    print('ps={0}'.format(ps))

    c.close()
    connection.close()
